package studentSysterm;

import java.util.ArrayList;
import java.util.Scanner;


public class App {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> list = new ArrayList<>();

        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            String choose = sc.next();
            switch (choose) {
                case "1": {
                    login(list);
                    break;
                }
                case "2": {
                    register(list);
                    break;
                }
                case "3": {
                    forgetpsaaword(list);
                    break;
                }
                case "4": {
                    System.out.println("退出");
                    System.exit(0);

                }
                default: {
                    System.out.println("没有这个选项");
                }
            }
        }


    }

    public static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        while (true) {
            System.out.println("请输入用户名");
            username= sc.next();
            boolean flag1 = checkusername(username);
            if (!flag1) {
                System.out.println("用户名格式不正确，请重新输入");
                continue;
            }
            //检查唯一性
            boolean contains = contains(list, username);
            if (contains) {
                System.out.println("用户名已存在，请重新输入");
            } else {
                break;
            }
        }

        String password1;
        while (true) {
            System.out.println("请输入密码");
            password1 = sc.next();
            System.out.println("请再次输入密码");
            String password2 = sc.next();
            if (password1.equals(password2)) {
                break;
            } else {
                System.out.println("两次密码输入不一致");
            }
        }

        String phone;
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            boolean flag3 = checkphone(phone);
            if (flag3) {
                break;
            } else {
                System.out.println("手机号格式不符，请重新输入");
            }
        }

        String personid;
        while (true) {
            System.out.println("请输入身份证号");
            personid = sc.next();
            boolean flag4 = checkpersonid(personid);
            if(flag4){
                break;
            }
            else {
                System.out.println("身份证号格式不符，请重新输入");

            }
        }

        User user= new User(username,password1,phone,personid);
        list.add(user);
        System.out.println("注册成功");
        printlist(list);
    }




    public static void login(ArrayList<User> list) {
        System.out.println("1.登录");

    }

    public static void forgetpsaaword(ArrayList<User> list) {
        System.out.println("3.忘记密码");

    }

    //检查用户名唯一性
    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (username.equals(user.getUsername())) {
                return true;
            }

        }
        return false;
    }

    //检查用户名格式
    private static boolean checkusername(String username) {
        //长度3-15，只能数字加字母，不能纯数字
        if (username.length() < 3 || username.length() > 15) {

            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }

    private static boolean checkphone(String phone) {
        if (phone.startsWith("0")) {
            return false;
        }

        if (phone.length() != 11) {
            return false;
        }
        for (int i = 0; i < phone.length(); i++) {
            char c = phone.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                return false;
            }
        }
        return true;
    }

    private static boolean checkpersonid(String personid) {
        if (personid.startsWith("0")) {
            return false;
        }

        if (personid.length() != 18) {
            return false;
        }

        for (int i = 0; i < 17; i++) {
            char c = personid.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }

        char c = personid.charAt(17);
        if(!((c >= '0' && c <= '9')||(c=='z')||(c=='Z'))){
            return false;
        }

        return true;
    }

    private static void printlist(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getPhonenumber()+" "+user.getPersonid());
        }
    }
}
